package com.example.childpositionapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.location.LocationClient;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolygonOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.model.LatLng;
import com.bumptech.glide.load.Option;
import com.example.childpositionapp.model.Fence;
import com.example.childpositionapp.model.Receive;
import com.example.childpositionapp.network.Tool;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class AddFenceActivity extends AppCompatActivity {
    private MapView mMapView = null;
    BaiduMap mBaiduMap = null;
    LocationClient mLocationClient = null;
    StringBuilder lon = new StringBuilder();
    StringBuilder lat = new StringBuilder();
    Button bt1;
    Button bt2;
    LatLng latLng = null;
    List<LatLng> points = new ArrayList<>();
    PolygonOptions mPolygonOptions=null;
    List<OverlayOptions> list=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_fence);
        Intent intent = getIntent();
        mMapView = (MapView) findViewById(R.id.bmap);
        mBaiduMap = mMapView.getMap();

        //标记处当前坐标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.weizhi);
        latLng = new LatLng(intent.getDoubleExtra("lat", 0), intent.getDoubleExtra("lon", 0));
        OverlayOptions option = new MarkerOptions()
                .position(latLng)
                .icon(bitmap);
        list.add(option);
        mBaiduMap.addOverlay(option);
        MapStatus mMapStatus = new MapStatus.Builder().target(latLng).zoom(20).build();
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        mBaiduMap.setMapStatus(mapStatusUpdate);

        //数据的前括号
        lon.append("[");
        lat.append("[");

        mMapView = (MapView) findViewById(R.id.bmap);
        bt1 = findViewById(R.id.bt_quxiao);
        bt2 = findViewById(R.id.bt_queding);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fence fence = new Fence(lon.substring(0, lon.length() - 1) + "]", lat.substring(0, lat.length() - 1) + "]");
                Log.d("222222222222222222", new Gson().toJson(fence));
                Tool.addFence(new Gson().toJson(fence), new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(AddFenceActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        Receive receive=new Gson().fromJson(response.body().string(),Receive.class);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (receive.getCode()==200){
                                    Toast.makeText(AddFenceActivity.this, "增加成功", Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(AddFenceActivity.this, receive.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        finish();
                    }
                });
            }
        });
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                lon.append(latLng.longitude ).append(",");
                lat.append(latLng.latitude ).append(",");
                points.add(latLng);
                if (points.size()>=3){
                    mPolygonOptions = new PolygonOptions()
                            .points(points)
                            .fillColor(0xAAff7575) //填充颜色
                            .stroke(new Stroke(5, 0xAAff7575));//边框宽度和颜色
                    mBaiduMap.clear();
                    mBaiduMap.addOverlays(list);
                    mBaiduMap.addOverlay(mPolygonOptions);
                }

                BitmapDescriptor bitmap2 = BitmapDescriptorFactory
                        .fromResource(R.drawable.weizhi2);
                OverlayOptions option = new MarkerOptions()
                        .position(latLng)
                        .icon(bitmap2);
                list.add(option);
                mBaiduMap.addOverlay(option);
            }

            @Override
            public void onMapPoiClick(MapPoi mapPoi) {
                String name = mapPoi.getName();
                LatLng latLng = mapPoi.getPosition();
                lon.append(latLng.longitude).append(",");
                lat.append(latLng.latitude).append(",");
                points.add(latLng);
                if (points.size()>=3){
                    mPolygonOptions = new PolygonOptions()
                            .points(points)
                            .fillColor(0xAAff7575) //填充颜色
                            .stroke(new Stroke(5, 0xAAff7575));//边框宽度和颜色
                    mBaiduMap.clear();
                    mBaiduMap.addOverlays(list);
                    mBaiduMap.addOverlay(mPolygonOptions);
                }
                BitmapDescriptor bitmap2 = BitmapDescriptorFactory
                        .fromResource(R.drawable.weizhi2);
                OverlayOptions option = new MarkerOptions()
                        .position(latLng)
                        .icon(bitmap2);
                list.add(option);
                mBaiduMap.addOverlay(option);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
    }
}